package com.valuegroup.smarttodo.plugins.pickdialog;

import android.content.Context;

import com.valuegroup.smarttodo.R;
import com.valuegroup.smarttodo.plugins.pickdialog.genview.WheelGeneralAdapter;
import com.valuegroup.smarttodo.plugins.pickdialog.view.WheelView;
import com.valuegroup.smarttodo.plugins.pickdialog.bean.DateType;

import java.util.Date;

/**
 * 日期选择器
 * Created by codbking on 2016/8/10.
 */
class DatePicker extends BaseWheelPick {

    private static final String TAG = "WheelPicker";

    private WheelView yearView;
    private WheelView monthView;
    private WheelView dayView;
    private WheelView hourView;
    private WheelView minuteView;

    private Integer[] yearArr, quarterArr, mothArr, dayArr, hourArr, minutArr;
    private DatePickerHelper datePicker;

    public DateType type = DateType.TYPE_ALL;

    //开始时间
    private Date startDate = new Date();
    private Object startData;
    //年分限制，默认上下5年
    private int yearLimt = 5;

    private OnChangeLisener onChangeLisener;
    private int selectDay;
    private int selectMinute;

    //选择时间回调
    public void setOnChangeLisener(OnChangeLisener onChangeLisener) {
        this.onChangeLisener = onChangeLisener;
    }

    public DatePicker(Context context, DateType type) {
        super(context);
        if (this.type != null) {
            this.type = type;
        }
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public void setYearLimt(int yearLimt) {
        this.yearLimt = yearLimt;
    }

    //初始化值
    public void init() {

        this.minuteView = findViewById(R.id.minute);
        this.hourView = findViewById(R.id.hour);
        this.dayView = findViewById(R.id.day);
        this.monthView = findViewById(R.id.month);
        this.yearView = findViewById(R.id.year);

        this.minuteView.setVisibility(GONE);
        this.hourView.setVisibility(GONE);
        this.dayView.setVisibility(GONE);
        this.monthView.setVisibility(GONE);
        this.yearView.setVisibility(GONE);

        // 按类型显示项目
        switch (type) {
            case TYPE_ALL:
                this.yearView.setVisibility(VISIBLE);
                this.monthView.setVisibility(VISIBLE);
                this.dayView.setVisibility(VISIBLE);
                this.hourView.setVisibility(VISIBLE);
                this.minuteView.setVisibility(VISIBLE);
            case TYPE_YMDH:
                this.yearView.setVisibility(VISIBLE);
                this.monthView.setVisibility(VISIBLE);
                this.dayView.setVisibility(VISIBLE);
                this.hourView.setVisibility(VISIBLE);
                break;
            case TYPE_YM:
                this.yearView.setVisibility(VISIBLE);
                this.monthView.setVisibility(VISIBLE);
                break;
            case TYPE_YMD:
                this.yearView.setVisibility(VISIBLE);
                this.monthView.setVisibility(VISIBLE);
                this.dayView.setVisibility(VISIBLE);
                break;
            case TYPE_Y:
                this.yearView.setVisibility(VISIBLE);
                break;
            case TYPE_HM:
                this.hourView.setVisibility(VISIBLE);
                this.minuteView.setVisibility(VISIBLE);
                break;
            case TYPE_M:
                this.minuteView.setVisibility(VISIBLE);
                break;
            case TYPE_CUSTOM_MINUTE_INTERVAL_5:
                this.minuteView.setVisibility(VISIBLE);
                break;
            case TYPE_CUSTOM_QUARTER:
                this.monthView.setVisibility(VISIBLE);
                break;
            case TYPE_CUSTOM_YEAR_QUARTER:
                this.monthView.setVisibility(VISIBLE);
                this.yearView.setVisibility(VISIBLE);
                break;
            case TYPE_CUSTOM_YEAR_WEEK:
                this.yearView.setVisibility(VISIBLE);
                this.minuteView.setVisibility(VISIBLE);
                break;
            case TYPE_CUSTOM_WEEK:
                this.hourView.setVisibility(VISIBLE);
                break;
            case TYPE_CUSTOM_DAY:
                this.dayView.setVisibility(VISIBLE);
                break;
            case TYPE_CUSTOM_NUMBER:
                this.minuteView.setVisibility(VISIBLE);
                break;
        }

        datePicker = new DatePickerHelper();

        // 设定初始值
        if (type.name().contains("CUSTOM")) {
            datePicker.setStartData(startData, yearLimt, type);
        } else {
            datePicker.setStartDate(startDate, yearLimt);
        }

        dayArr = datePicker.genDay();
        yearArr = datePicker.genYear();
        quarterArr = datePicker.genQuarter();
        mothArr = datePicker.genMonth();
        hourArr = datePicker.genHour();
        minutArr = datePicker.genMinut();

        if (type.name().contains("CUSTOM")) {
            // 自定义类型数据初始化
            if (type == DateType.TYPE_CUSTOM_QUARTER || type == DateType.TYPE_CUSTOM_YEAR_QUARTER) {
                mothArr = datePicker.genQuarter();      // 季度处理
            } else if (type == DateType.TYPE_CUSTOM_WEEK) {
                hourArr = datePicker.genArr(7, true);// 星期处理
            } else if (type == DateType.TYPE_CUSTOM_MINUTE_INTERVAL_5) {
                minutArr = datePicker.genMinutInterval5();    // 5-55 分钟间隔为5
            } else if (type == DateType.TYPE_CUSTOM_NUMBER) {
                minutArr = datePicker.genArr(100, 5, false);// 默认100次，间隔为5
            } else if (type == DateType.TYPE_CUSTOM_DAY) {
                dayArr = datePicker.genArr(31, false);//默认31天
            }
        }

        // 初始化选择控件
        setWheelListener(yearView, yearArr, false);
        setWheelListener(monthView, mothArr, true);
        setWheelListener(dayView, dayArr, true);
        setWheelListener(hourView, hourArr, true);
        setWheelListener(minuteView, minutArr, true);

        // 选中默认值
        yearView.setCurrentItem(datePicker.findIndextByValue(datePicker.getToady(DatePickerHelper.Type.YEAR), yearArr));
        monthView.setCurrentItem(datePicker.findIndextByValue(datePicker.getToady(DatePickerHelper.Type.MOTH), mothArr));
        dayView.setCurrentItem(datePicker.findIndextByValue(datePicker.getToady(DatePickerHelper.Type.DAY), dayArr));
        hourView.setCurrentItem(datePicker.findIndextByValue(datePicker.getToady(DatePickerHelper.Type.HOUR), hourArr));
        minuteView.setCurrentItem(datePicker.findIndextByValue(datePicker.getToady(DatePickerHelper.Type.MINUTE), minutArr));

    }


    protected String[] convertData(WheelView wheelView, Integer[] data) {
        if (wheelView == yearView) {
            return datePicker.getDisplayValue(data, "年");
        } else if (wheelView == monthView) {
            if (type == DateType.TYPE_CUSTOM_QUARTER || type == DateType.TYPE_CUSTOM_YEAR_QUARTER) {
                return datePicker.getDisplayValue(data, "季度");
            } else {
                return datePicker.getDisplayValue(data, "月");
            }
        } else if (wheelView == dayView) {
            return datePicker.getDisplayValue(data, "日");
        } else if (wheelView == hourView) {
            if (type == DateType.TYPE_CUSTOM_WEEK) {
                return datePicker.getWeeks();
            } else {
                return datePicker.getDisplayValue(data, "时");
            }
        } else if (wheelView == minuteView) {
            if (type == DateType.TYPE_CUSTOM_NUMBER) {
                return datePicker.getDisplayValue(data, "次");
            } else if (type == DateType.TYPE_CUSTOM_YEAR_WEEK) {
                return datePicker.getDisplayValue(data, "第", "周");
            } else if (type == DateType.TYPE_M) {
                return datePicker.getDisplayValue(data, "分钟");
            } else {
                return datePicker.getDisplayValue(data, "分");
            }
        }
        return new String[0];
    }

    @Override
    protected int getLayout() {
        return R.layout.cbk_wheel_picker;
    }

    @Override
    protected int getItemHeight() {
        return dayView.getItemHeight();
    }


    @Override
    protected void setData(Object[] datas) {
    }

    private void setChangeDaySelect(int year, int moth) {
        dayArr = datePicker.genDay(year, moth);
        WheelGeneralAdapter adapter = (WheelGeneralAdapter) dayView.getViewAdapter();
        adapter.setData(convertData(dayView, dayArr));

        int indxt = datePicker.findIndextByValue(selectDay, dayArr);
        if (indxt == -1) {
            dayView.setCurrentItem(0);
        } else {
            dayView.setCurrentItem(indxt);
        }
    }

    private void setChangeWeekSelect(int year) {
        int max = DateUtils.getMaxWeekNumOfYear(year);
        minutArr = datePicker.genArr(max, false);
        WheelGeneralAdapter adapter = (WheelGeneralAdapter) minuteView.getViewAdapter();
        adapter.setData(convertData(minuteView, minutArr));

        int indxt = datePicker.findIndextByValue(selectMinute, minutArr);
        if (indxt == -1) {
            minuteView.setCurrentItem(0);
        } else {
            minuteView.setCurrentItem(indxt);
        }
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {

        int year = yearArr[yearView.getCurrentItem()];
        int moth = mothArr[monthView.getCurrentItem()];
        int day = dayArr[dayView.getCurrentItem()];
        int hour = hourArr[hourView.getCurrentItem()];
        int minut = minutArr[minuteView.getCurrentItem()];

        if (type.getFormat().contains("dd") && (wheel == yearView || wheel == monthView)) {
            // 含年月日，变化年份和月份的时候日期的范围需要变化
            setChangeDaySelect(year, moth);
        } else if (type == DateType.TYPE_CUSTOM_YEAR_WEEK && wheel == yearView) {
            // 获取某年第几周，变化年份时周对应要发生变化
            setChangeWeekSelect(year);
        } else {
            selectDay = day;
            selectMinute = minut;
        }

        if (onChangeLisener != null) {
            onChangeLisener.onChanged(DateUtils.getDate(year, moth, day, hour, minut));
        }

    }

    @Override
    public void onScrollingStarted(WheelView wheel) {
    }

    @Override
    public void onScrollingFinished(WheelView wheel) {
    }


    //获取选中日期
    public Date getSelectDate() {

        int year = yearArr[yearView.getCurrentItem()];
        int moth = mothArr[monthView.getCurrentItem()];
        int day = dayArr[dayView.getCurrentItem()];
        int hour = hourArr[hourView.getCurrentItem()];
        int minut = minutArr[minuteView.getCurrentItem()];

        return DateUtils.getDate(year, moth, day, hour, minut);

    }

    //获取选中季度（季度是依据月份来定义，例如：1月、2月、3月为一季度，以此类推）
    public Object getSelectQuarter() {
        return mothArr[monthView.getCurrentItem()];
    }

    //获取选中的星期
    public Object getSelectWeek() {
        return hourArr[hourView.getCurrentItem()];
    }

    //获取选中的天
    public Object getSelectDay() {
        return dayArr[dayView.getCurrentItem()];
    }

    public Object getSelectCount() {
        return minutArr[minuteView.getCurrentItem()];
    }

    public Object getMinuteInterval5() {
        return minutArr[minuteView.getCurrentItem()];
    }

    public Object getSelectYearQuarter() {
        int year = yearArr[yearView.getCurrentItem()];
        int moth = mothArr[monthView.getCurrentItem()];
        //201904
        return year * 100 + moth;
    }

    public Object getSelectYearWeek() {
        int year = yearArr[yearView.getCurrentItem()];
        int minut = minutArr[minuteView.getCurrentItem()];
        //201924
        return year * 100 + minut;
    }

    public void setStartData(Object startData) {
        this.startData = startData;
    }
}
